home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 315.adf / Surf / scrnops.c < prev    next >
C/C++ Source or Header  |  1990-02-14  |  9KB  |  406 lines

  1. #include "scrnio.ih"
  2. /*
  3.  * if manx supports rand, these ifdefs could be removed (somewhat)
  4.  */
  5. #ifdef MANX
  6. extern double ran(); /* not in manx  math.h */
  7. #include <functions.h>
  8. #else
  9. extern int rand();
  10. #endif
  11.  
  12. #include "scrndef.h"
  13. #include "scrnio.h"
  14. #include "gadgetdef.h"
  15. #include "menudef.h"
  16.  
  17. #include "bezpt.h"
  18. #include "revolve.h"
  19. #include "control.h"
  20. #include "poly.h"
  21.  
  22.  
  23. long BackColor = DefBkPlane;
  24. short DitherPower = 2;/* default gray levels = 4 */
  25. static UWORD *GrayPat = null;
  26. short DitherLevels, DitherMask;
  27. #define RowPower 4  /* 2**4 = 16 rows */
  28. #define DitherLoc(LEVEL) ((LEVEL)<<RowPower)
  29. /*
  30.  * free memory associated with
  31.  * old dither patterns;
  32.  */
  33. void FreeOldDither() {
  34.     if( GrayPat ) free(GrayPat);
  35.     GrayPat = null;
  36. }
  37. /*
  38.  * create grey level patterns
  39.  */
  40. bool AllocDither()
  41. {
  42.     int i;
  43.     float reallevels;
  44.     if( GrayPat) return(true);
  45.  
  46.     DitherLevels = 1 << DitherPower;
  47.     reallevels = (float) DitherLevels - 0.5;
  48.     DitherMask = DitherLevels -1;
  49.     GrayPat = (UWORD *)malloc(DitherLoc(DitherLevels)*sizeof(UWORD));
  50.     if( !GrayPat ) {
  51.         OutErr("not enough memory for options chosen");
  52.         return(false);
  53.     }
  54.     ColorMax = (NumColors -1) * DitherLevels +1;
  55.     /*
  56.      * compute gray values for each grey level
  57.      */
  58.     for( i = 0; i < DitherLevels; i++ ) {
  59.         /*
  60.          * compute gray values for each row
  61.          */
  62.         int j;
  63. #ifdef MANX
  64.         float fracti;
  65.         fracti = (float)i/ reallevels;
  66.         for( j = 0; j < DitherLoc(1); j++ ) {
  67.             register long k, x;
  68.             for( k = 16, x = 0; k--; ) {
  69.                 x <<= 1;
  70.                 x |= (ran() < fracti) ? 1: 0;
  71.             }
  72.             GrayPat[DitherLoc(i)+j] = x;
  73.         }
  74. #else MANX
  75.         for( j = 0; j < DitherLoc(1); j++ ) {
  76.             register long k, x;
  77.             for( k = 16, x = 0; k--; ) {
  78.                 x <<= 1;
  79.                 x |= ((rand()%DitherLevels)< i) ? 1: 0;
  80.             }
  81.             GrayPat[DitherLoc(i)+j] = x;
  82.         }
  83. #endif MANX
  84.     }
  85.     return(true);
  86. }
  87.  
  88.  
  89. void SetMono( maxrval, maxgval, maxbval )
  90.     short maxrval,
  91.           maxgval,
  92.           maxbval;
  93. {
  94.     long i;
  95.     short range;
  96.     long rval, gval, bval;
  97.  
  98.     range = (NumColors -1) & 0x1f;  /* max 32 colours */
  99.     for( i = 0; i <= range; i++ ) {
  100.         rval = (maxrval * i )/range;
  101.         gval = (maxgval * i )/range;
  102.         bval = (maxbval * i )/range;
  103.  
  104.         SetRGB4( &(SurfScrn->ViewPort),  i, rval, gval, bval );
  105.     }
  106. }
  107.  
  108.  
  109. void SetRainbow()
  110. {
  111.     long i;
  112.     short range;
  113.     long rval, gval, bval;
  114.  
  115.     range = NumColors>> 1;
  116.     /*
  117.      * can't do a rainbow with only 2 colors
  118.      */
  119.     if( range < 2) {
  120.         return;
  121.     }
  122.  
  123.     for( i = 0; i < range; i++ ) {
  124.         long diff;
  125.  
  126.         diff = (0xf * i )/(range-1);
  127.         rval = 0xf - diff;
  128.         bval = diff;
  129.         gval = 0xf;
  130.         SetRGB4( &(SurfScrn->ViewPort), i, rval, gval, bval);
  131.     }
  132.     for( i = 0; i < range; i++ ) {
  133.         long diff;
  134.  
  135.         diff = (0xf * i )/(range-1);
  136.         rval = diff;
  137.         bval = 0xf;
  138.         gval = 0xf - diff;
  139.         SetRGB4( &(SurfScrn->ViewPort), i+range, rval, gval, bval);
  140.     }
  141.     SetRGB4( &(SurfScrn->ViewPort), 0L, 0L, 0L, 0L);
  142. }
  143.  
  144. /*
  145.  * set colours for hourglass pointer
  146.  */
  147. SetHourGlassCol()
  148. {
  149.     SetRGB4( &(SurfScrn->ViewPort),17L, 6L, 2L, 3L );
  150.     SetRGB4( &(SurfScrn->ViewPort),18L, 0L, 0L, 0L );
  151.     SetRGB4( &(SurfScrn->ViewPort),19L, 9L, 7L, 6L );
  152. }
  153.  
  154.  
  155.  
  156. void ClrWindow(drawaxis)
  157. bool drawaxis;
  158. {
  159.     long BkColAdj; /* background color adjusted for number of bit planes */
  160.  
  161.     BkColAdj = (BackColor * NumColors) / 32;
  162.     SetRast(rp, BackColor);  /* clear the window to colour 0 */
  163.     SetAPen(rp, WinFgCol );
  164.     /*
  165.      * Draw axis on screen
  166.      */
  167.     if( drawaxis) {
  168.         Move( rp, 0, WinVOrig);    /* x axis */
  169.         Draw( rp, (long)SurfWinDef.Width, (long)WinVOrig );
  170.  
  171.         Move( rp, WinHOrig, 0);     /* y axis */
  172.         Draw( rp, (long)WinHOrig, (long)SurfWinDef.Height );
  173.     }
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180. void DrawLine( x1, y1, x2, y2, mode )
  181. int x1, y1, x2, y2;
  182. int mode;
  183. {
  184.     SetDrMd( rp, mode );
  185.     Move( rp, (long)UCntrX(x1), (long)UCntrY(y1));
  186.     Draw(rp, (long)UCntrX(x2), (long)UCntrY(y2) );
  187. }
  188.  
  189.  
  190.  
  191. void PaintPoint(x,y,forecolor)
  192.     short x, y;
  193.     float forecolor;
  194. {
  195.     long shade;
  196.     shade = forecolor * (float) (NumColors-1);
  197.  
  198.     if( shade >= NumColors) {
  199.         shade = NumColors-1;
  200.     }
  201.     else if ( shade < 0 ) {
  202.         shade = 0;
  203.     }
  204.  
  205.     SetAPen( rp, shade );
  206.     WritePixel( rp, (long)UCntrX(x), (long)UCntrY(y));
  207. }
  208.  
  209.  
  210.  
  211. void DrawPnt( x, y, op )
  212. int x, y, op;
  213. {
  214.     x = UCntrX(x);
  215.     y = UCntrY(y);
  216.     SetDrMd(rp, op );
  217.     RectFill( rp, (long)x, (long)y, (long)x, (long)y);
  218. }
  219.  
  220.  
  221. void DrawSqr(x, y, op )
  222. int x, y, op;
  223. {
  224.     x = UCntrX(x);
  225.     y = UCntrY(y);
  226.     SetDrMd(rp, op );
  227.     RectFill( rp, x - 2L, y -2L, x+ 2L, y+2L );
  228. }
  229.  
  230.  
  231. void DrawBox( x, y, op)
  232. int x, y, op;
  233. {
  234.     x = UCntrX(x);
  235.     y = UCntrY(y);
  236.     SetDrMd(rp, op );
  237.     RectFill( rp, x - 4L, y -4L, x+ 4L, y+4L );
  238.     RectFill( rp, x - 3L, y -3L, x+ 3L, y+3L );
  239. }
  240.  
  241.  
  242. void DrawRhomShade( poly)
  243. Rhomboid *poly;
  244. {
  245.     int i;
  246.     int shade;
  247.     long backcolor, forecolor;
  248.     static UWORD FullBits = 0xffff;
  249.  
  250.     shade = (int)((poly->intensity) * ColorMax);
  251.  
  252.     if( shade >= ColorMax -1 ) {
  253.         shade = 0;
  254.         backcolor = NumColors -1;
  255.     }
  256.     else {
  257.         backcolor = shade >> DitherPower;
  258.         forecolor = backcolor +1;
  259.         if( forecolor >= NumColors ) {
  260.             forecolor = backcolor;
  261.         }
  262.     }
  263.     SetDrMd( rp, JAM2);
  264.     SetAPen( rp, forecolor );
  265.     SetBPen( rp, backcolor );
  266.     SetAfPt( rp, &GrayPat[DitherLoc((shade & DitherMask))], RowPower);
  267.  
  268.     AreaMove( rp, UCntrX(poly->pt[0].x), UCntrY(poly->pt[0].y));
  269.  
  270.     for( i = 1; i < 4; i++ ) {
  271.         AreaDraw( rp, UCntrX(poly->pt[i].x), UCntrY(poly->pt[i].y) );
  272.     }
  273.     AreaEnd(rp);
  274.  
  275.     SetAfPt( rp, &FullBits, 0);  /* reset back to solid */
  276. }
  277.  
  278. void DrawRhomFrame( inlist )
  279. ScrnPair inlist[];
  280. {
  281.     int i;
  282.  
  283.     SetDrMd( rp, JAM1);
  284.     SetAPen( rp, 0L );
  285.     SetOPen( rp, WinFgCol );
  286.  
  287.     AreaMove( rp, UCntrX(inlist[0].x), UCntrY(inlist[0].y));
  288.  
  289.     for( i = 1; i < 4; i++ ) {
  290.         AreaDraw( rp, UCntrX(inlist[i].x), UCntrY(inlist[i].y) );
  291.     }
  292.     AreaEnd(rp);
  293.     BNDRYOFF( rp ); /* turn off outlining */
  294. }
  295.  
  296.  
  297.  
  298.  
  299. SwitchBox()
  300. {
  301.     struct IntuiMessage mycopy,
  302.                         *orig;
  303.  
  304.     RefreshGadgets(SurfWinDef.FirstGadget, SurfWin, NULL );
  305.  
  306.     while(1) {
  307.         long wakeupmask;
  308.  
  309.         wakeupmask = Wait( SignalMask );
  310.         /*
  311.          * for now, we ignore the wakeupmask,
  312.          * just read messages from each. if I notice a performance problem,
  313.          * I'll fix it then
  314.          */
  315.  
  316.         /*
  317.          * handle messages for the control window
  318.          */
  319.         while( orig =(struct IntuiMessage *) GetMsg( CntrlWin->UserPort ) ) {
  320.  
  321.             mycopy = *orig;
  322.             ReplyMsg( orig );
  323.  
  324.             switch( mycopy.Class ) {
  325.                 case MENUPICK:
  326.                     MenuHandler( mycopy.Code );
  327.                     break;
  328.  
  329.                 case GADGETUP:
  330.                     GadgetHandler( (struct Gadget*)mycopy.IAddress );
  331.                     break;
  332.  
  333.                 case CLOSEWINDOW:
  334.                     return;
  335.  
  336.                 default:
  337.                     break;
  338.             }
  339.         }
  340.         /*
  341.          * handle the button window
  342.          */
  343.         while( orig =(struct IntuiMessage *) GetMsg( GadWin->UserPort ) ) {
  344.  
  345.             mycopy = *orig;
  346.             ReplyMsg( orig );
  347.  
  348.             switch( mycopy.Class ) {
  349.                 case GADGETUP:
  350.                     GadgetHandler( (struct Gadget*)mycopy.IAddress );
  351.                     RefreshGadgets(SurfWinDef.FirstGadget, SurfWin, NULL );
  352.                     break;
  353.  
  354.                 default:
  355.                     break;
  356.             }
  357.         }
  358.  
  359.         /*
  360.          * handle messages for the other window
  361.          */
  362.         while( orig =(struct IntuiMessage *) GetMsg( SurfWin->UserPort ) ) {
  363.  
  364.             mycopy = *orig;
  365.             ReplyMsg( orig );
  366.  
  367.             switch( mycopy.Class ) {
  368.                 case MOUSEBUTTONS:
  369.                     HandleMButtons(&mycopy);
  370.                     break;
  371.  
  372.                 case INTUITICKS:
  373.                     HandleTicks(&mycopy);
  374.                     break;
  375.  
  376.                 case MOUSEMOVE:
  377.                     break;
  378.  
  379.                 default:
  380.                     break;
  381.             }
  382.         }
  383.     }
  384. }
  385.  
  386. /*
  387.  * display error messages inside a requestor
  388.  */
  389. OutErr(errstr)
  390.     char *errstr;
  391. {
  392.     static struct IntuiText errtext =
  393.         { -1, -1, JAM1, 10, 10, NULL, NULL, NULL };
  394.     static struct IntuiText negtext =
  395.         { -1, -1, JAM1, 80, 20, NULL,(UBYTE *)"Onwards", NULL };
  396.  
  397.     errtext.IText = (UBYTE *)errstr;
  398.  
  399.     WBenchToFront();
  400.     AutoRequest(CntrlWin, &errtext, NULL, &negtext, NULL, NULL,
  401.         8*strlen(errstr)+ 40, 60 );
  402.     WindowToFront( CntrlWin );
  403.  
  404. }
  405.  
  406.